/**
 * 
 */
package xiaojian.toolkit.netty.handler.codec.http.response;

import io.netty.channel.Channel;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.HttpResponseStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import xiaojian.toolkit.base.SendbackResponseHelper;
import xiaojian.toolkit.bean.AbstractCommonBean;
import xiaojian.toolkit.bean.tlv.encode.TLVEncoderOfBean;
import xiaojian.toolkit.netty.util.NettyConstants;
import xiaojian.toolkit.util.ByteUtils;
import xiaojian.toolkit.util.Propertyable;

public class TLVSendbackResponseHelper implements SendbackResponseHelper {

	private static final Logger logger = LoggerFactory
			.getLogger(TLVSendbackResponseHelper.class);

	private NettyHttpResponseBuilder httpResponseBuilder;
	private NettyHttpResponseSender httpResponseSender;
	private TLVEncoderOfBean beanEncoder;

	public TLVEncoderOfBean getBeanEncoder() {
		return beanEncoder;
	}

	public void setBeanEncoder(TLVEncoderOfBean beanEncoder) {
		this.beanEncoder = beanEncoder;
	}

	public NettyHttpResponseBuilder getHttpResponseBuilder() {
		return httpResponseBuilder;
	}

	public void setHttpResponseBuilder(
			NettyHttpResponseBuilder httpResponseBuilder) {
		this.httpResponseBuilder = httpResponseBuilder;
	}

	public NettyHttpResponseSender getHttpResponseSender() {
		return httpResponseSender;
	}

	public void setHttpResponseSender(NettyHttpResponseSender httpResponseSender) {
		this.httpResponseSender = httpResponseSender;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.skymobi.http.util.SendbackResponseHelper#makeAndSendback(java.lang
	 * .Object, com.skymobi.util.Propertyable)
	 */
	public void makeAndSendback(Object bean, Propertyable propertyable) {

		if (logger.isDebugEnabled()) {
			logger.debug("sendback bean {} ", bean);
		}

		DefaultNettyResponseSource source = new DefaultNettyResponseSource();
		source.setStatus(HttpResponseStatus.OK);
		source.setContentType("application/x-tar");
		byte[] bytes = ByteUtils.union(beanEncoder.encode(
				bean,
				beanEncoder.getEncodeContextFactory().createEncodeContext(
						bean.getClass(), null)));

		if (logger.isDebugEnabled()) {
			logger.debug("sendback bean as Hex \r\n{} ",
					ByteUtils.bytesAsTLVHexString(bytes, 8096));
		}

		source.setContent(bytes);
		source.setContentLength(bytes.length);

		HttpResponse response = httpResponseBuilder.build(source);
		if (bean instanceof AbstractCommonBean) {
			Object request = ((AbstractCommonBean) bean)
					.getProperty(NettyConstants.HTTP_REQUEST);
			if (request != null) {
				String uuid = ((HttpRequest) request).headers().get(
						NettyConstants.HEADER_UUID);
				if (uuid != null)
					response.headers().set(NettyConstants.HEADER_UUID, uuid);
				response.headers().set(
						HttpHeaders.Names.CONNECTION,
						((HttpRequest) request).headers().get(
								HttpHeaders.Names.CONNECTION));
			}
		}
		httpResponseSender.sendResponse((Channel) propertyable
				.getProperty(NettyConstants.PROPERTY_CHANNEL), response);

	}
}
